//! \file messagequeue.hpp
//! \brief Purewell Library Message Queue Header
//! \author Yubin Lim (purewell@gmail.com)
//! \since 2008-01-25

#ifndef __MESSAGEQUEUE_HPP__
#define __MESSAGEQUEUE_HPP__

#include "file.hpp"

namespace purewell
{

//! \brief Message queue
class PWMessageQueue : public PWNamedFile
{
public:
	//! \brief Initialize message queue.
	//! \param[in] name Name.
	//! \param[in] omode Open mode.
	//! \param[in] perm Permittion.
	//! \param[in] maxmsg Max message count.
	//! \param[in] msgsize Message Size.
	//! \return On success, true.
	virtual bool initialize(const char* name, int omode, int perm,
		size_t maxmsg, size_t msgsize);

	//! \brief Initialize message queue.
	//! \param[in] name Name.
	//! \param[in] omode Open mode.
	//! \param[in] perm Permittion.
	//! \return On success, true.
	virtual bool initialize(const char* name, int omode, int perm);

	//! \brief Unlink message queue file.
	//! \return On success, true.
	virtual bool unlink(void);

	//! \brief Destroy.
	virtual void destroy(void);

public:	

	//! \brief Write data to message queue.
	//! \param[in] buf Buffer.
	//! \param[in] blen Buffer length.
	//! \param[in] priority Priority.
	//! \return Written bytes.
	virtual ssize_t send(const void* buf, size_t blen, size_t priority);

	//! \brief Write data to message queue in time.
	//! \param[in] buf Buffer.
	//! \param[in] blen Buffer length.
	//! \param[in] priority Priority.
	//! \param[in] msec Time limit(unit: millisecond)
	//! \return Written bytes.
	virtual ssize_t sendTimeLimit(const void* buf, size_t blen,
		size_t priority, size_t msec);

	//! \brief Read data from message queue.
	//! \param[out] buf Buffer.
	//! \param[in] blen Buffer length.
	//! \param[out] priority Priority.
	//! \return Read bytes.
	virtual ssize_t receive(void* buf, size_t blen, size_t* priority);

	//! \brief Read data from message queue in time.
	//! \param[out] buf Buffer.
	//! \param[in] blen Buffer length.
	//! \param[out] priority Priority.
	//! \param[in] msec Time limit(unit: millisecond)
	//! \return Read bytes.
	virtual ssize_t receiveTimeLimit(void* buf, size_t blen,
		size_t* priority, size_t msec);

	virtual ssize_t read(void* buf, size_t blen);
	virtual ssize_t write(const void* buf, size_t blen);

	//! \brief Get current message count.
	//! \return Current message count.
	size_t getCurrentCount(void) const;

	//! \brief Get max message count.
	//! \return Max message count.
	size_t getMaxCount(void) const;

	//! \brief Get message size.
	//! \return Message size.
	size_t getMessageSize(void) const;

	//! \brief Get status.
	//! \param[out] maxcount Max message count.
	//! \param[out] msgsize Message size.
	//! \param[out] curcount Message current count.
	bool getStatus(size_t* __restrict__ maxcount,
		size_t* __restrict__ msgsize,
		size_t* __restrict__ curcount) const;

public:
	explicit PWMessageQueue();
	virtual ~PWMessageQueue();

};

};

#endif//!__MESSAGEQUEUE_HPP__

